﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;

namespace ADOWrapper.ObjectResolver
{
    class GenericInstanceCreator<T> : IInstanceCreator<T>
    {

        Func<T> instanceProvider = null;

        public GenericInstanceCreator()
            : this(null) { }

        public GenericInstanceCreator(Func<T> instanceProvider)
        {
            this.instanceProvider = instanceProvider ?? this.ConstructDefaultProvider();
        }

        public Func<T> ConstructDefaultProvider()
        {
            Expression<Func<T>> expression = Expression.Lambda<Func<T>>(Expression.New(typeof(T)));
            return expression.Compile();
        }

        public T Resolve()
        {
            return instanceProvider();
        }
    }



}